<?php
namespace App\Http\Controllers;
use App\Services\MemberService;
use Session;
use App\Services\CenterService;
use App\Services\CommonService;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Redis;
use App\Models\SmsLogModel;

class CenterController extends Controller
{
    //绑定手机号码接口
    public function BindTelephone(){
        $member = new MemberService;
        $mid = intval($_POST['member_id']);
        $data['telephone'] = addslashes($_POST['telephone']);
        $data['modi_date'] = date('Y-m-d H:i:s');

        $code = intval($_POST['code']);

        SmsLogModel::checkCode($data['telephone'],$code);
        $res = $member->modifyMember($mid,$data);
        if($res){
            $_data = array('code'=>'2x000001','msg'=>'手机号码绑定成功');
        }else{
            $_data = array('code'=>'4x000001','msg'=>'手机号码绑定失败');
        }

        return response()->json($_data);
    }

    // 个人中心接口
    public function personalCenter($mid){
        $member = new MemberService;
        $userinfo = $member->getMemberInfoByid($mid);

        if(!empty($userinfo)){
            $center = new CenterService;
            $common = new CommonService;

            if($userinfo->id > 116 && $userinfo->nick == base64_encode(base64_decode($userinfo->nick))){
                $userinfo->nick = base64_decode($userinfo->nick);
            }
            $d_address = $center->getDefaultAddress($mid);
            if(!empty($d_address)){
                $province = $common->getPcInfoById($d_address->province);
                $d_address->province_name = $province->name;

                $city = $common->getPcInfoById($d_address->city);
                $d_address->city_name = $city->name;

                $county = $common->getPcInfoById($d_address->county);
                $d_address->county_name = $county->name;
            }else{
                $d_address = array();
            }

            $_data = array('code'=>'2x000001','userinfo'=>$userinfo,'d_address'=>$d_address);
        }else{
            $_data = array('code'=>'3x000001','msg'=>'请登录','url'=>env('APP_URL'));
        }

        return response()->json($_data);
    }

    // 获取收货地址列表
    public function getAddress($mid){
        if(!$mid){
            $_data = array('code'=>'3x000001','msg'=>'请登录','url'=>env('APP_URL'));
        }else{
            $center = new CenterService;
            $common = new CommonService;

            $addressList = $center->getAddressList($mid);
            foreach($addressList as $itm=>$lst){
                $province = $common->getPcInfoById($lst->province);
                $addressList[$itm]->province_name = $province->name;

                $city = $common->getPcInfoById($lst->city);
                $addressList[$itm]->city_name = $city->name;

                $county = $common->getPcInfoById($lst->county);
                $addressList[$itm]->county_name = $county->name;

                unset($province,$city,$county);
            }

            $_data = array('code'=>'2x000001','address_list'=>$addressList);
        }

        return response()->json($_data);
    }

    // 添加收货地址
    public function storeAddress(){

        date_default_timezone_set('PRC');
        $data['member_id'] = intval($_POST['member_id']);
        $data['name'] = addslashes($_POST['name']);
        $data['telephone'] = addslashes($_POST['telephone']);
        $data['province'] = intval($_POST['province']);
        $data['city'] = intval($_POST['city']);
        $data['county'] = intval($_POST['county']);
        $data['address'] = addslashes($_POST['address']);
        //$data['zip_code'] = intval($_POST['zip_code']);
        $data['add_date'] = date('Y-m-d H:i:s');
        $center = new CenterService;

        $default_address = $center->getDefaultAddress(intval($_POST['member_id']));
        if(empty($default_address)){
            $data['is_default'] = 'yes';
        }

        $res = $center->insertAddress($data);
        if($res){
            $_data = array('code'=>'2x000001','msg'=>'添加收货地址成功');
        }else{
            $_data = array('code'=>'4x000001','msg'=>'添加收货地址失败');
        }
        return response()->json($_data);
    }

    // 更新收货地址
    public function updateAddress(){

        date_default_timezone_set('PRC');

        $id = intval($_POST['id']);
        $data['name'] = addslashes($_POST['name']);
        $data['telephone'] = addslashes($_POST['telephone']);
        $data['province'] = intval($_POST['province']);
        $data['city'] = intval($_POST['city']);
        $data['county'] = intval($_POST['county']);
        $data['address'] = addslashes($_POST['address']);
        $data['zip_code'] = intval($_POST['zip_code']);
        $data['update_date'] = date('Y-m-d H:i:s');

        $center = new CenterService;
        $res = $center->updateAddress($id,$data);
        if($res){
            $_data = array('code'=>'2x000001','msg'=>'更新收货地址成功');
        }else{
            $_data = array('code'=>'4x000001','msg'=>'更新收货地址失败');
        }

        return response()->json($_data);
    }

    // 设置为默认地址
    public function setDefaultAddress($id,$mid){
        if(!$mid){
            $_data = array('code'=>'3x000001','msg'=>'请登录','url'=>env('APP_URL'));
        }else{
            $data['is_default'] = 'yes';
            $data['update_date'] = date('Y-m-d H:i:s');

            $center = new CenterService;
            $_res = $center->clearDefault($mid);
            if($_res){
                $res = $center->updateAddress($id,$data);
                if($res){
                    $_data = array('code'=>'2x000001','msg'=>'设置默认地址成功');
                }else{
                    $_data = array('code'=>'4x000001','msg'=>'设置默认地址失败');
                }
            }else{
                $_data = array('code'=>'3x000002','msg'=>'暂无默认地址');
            }
        }

        return response()->json($_data);
    }

    // 删除收货地址
    public function delAddress($id){

        $center = new centerService;
        $res = $center->delAddress($id);
        if($res){
            $_data = array('code'=>'2x000001','msg'=>'删除收货地址成功');
        }else{
            $_data = array('code'=>'4x000001','msg'=>'删除收货地址失败');
        }

        return response()->json($_data);
    }

    // 修改个人信息
    public function updatePersonalMsg(){
        date_default_timezone_set('PRC');
        $member = new MemberService;
        $id = intval($_POST['member_id']);
        $data['telephone'] = addslashes($_POST['telephone']);
        $data['nick'] = addslashes($_POST['nick']);
        $data['modi_date'] = date('Y-m-d H:i:s');

        $res = $member->modifyMember($id,$data);
        if($res){
            $userinfo['nick'] = $data['nick'];
            $userinfo['telephone'] = $data['telephone'];

            $_data = array('code'=>'2x000001','msg'=>'更新成功');
        }else{
            $_data = array('code'=>'4x000001','msg'=>'更新失败');
        }
        return response()->json($_data);
    }
}
